package org.itboys.pay.weixin;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.random.RandomUtils;


/**
 * 作者：江雄
 * 时间：2017年12月18日
 * 描述：企业付款
 */
public class Transfers {
	// 微信企业付款URL
	public static final String URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

	private String mchAppId;// 微信appid
	private String mchId;// 商户号
	private String nonceStr;//随机字符串。
	private String sign;// 签名
	private String signType="MD5";//签名方式
	private String partnerTradeNo;//商户订单号。
	private String openId;//用户openID。
	private String checkName;//是否校验真实姓名。非必选。NO_CHECK：不校验真实姓名；FORCE_CHECK：强校验真实姓名
	private String reUserName;//用户真实姓名。非必选配合上一个属性
	private Integer amount;//付款金额。分
	private String desc;//企业付款操作说明信息
	private String spbillCreateIp;//调用接口的机器Ip地址


	private String key;//商户KEY

	public Transfers() {
	}



	public Transfers(String mchAppId, String mchId, String partnerTradeNo, String openId, String checkName, String reUserName, Integer amount, String desc, String spbillCreateIp, String key) {
		this.mchAppId = mchAppId;
		this.mchId = mchId;
		this.partnerTradeNo = partnerTradeNo;
		this.openId = openId;
		this.checkName = checkName;
		this.reUserName = reUserName;
		this.amount = amount;
		this.desc = desc;
		this.spbillCreateIp = spbillCreateIp;
		this.key = key;
		this.nonceStr = RandomUtils.getClearRandomStr(32);;
		this.signType = "MD5";
	}

	public static String getURL() {
		return URL;
	}

	public String getMchAppId() {
		return mchAppId;
	}

	public void setMchAppId(String mchAppId) {
		this.mchAppId = mchAppId;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getSignType() {
		return signType;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public String getPartnerTradeNo() {
		return partnerTradeNo;
	}

	public void setPartnerTradeNo(String partnerTradeNo) {
		this.partnerTradeNo = partnerTradeNo;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getCheckName() {
		return checkName;
	}

	public void setCheckName(String checkName) {
		this.checkName = checkName;
	}

	public String getReUserName() {
		return reUserName;
	}

	public void setReUserName(String reUserName) {
		this.reUserName = reUserName;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getSpbillCreateIp() {
		return spbillCreateIp;
	}

	public void setSpbillCreateIp(String spbillCreateIp) {
		this.spbillCreateIp = spbillCreateIp;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * 完整的xml格式的参数 含签名。按ascii码排序
	 * 
	 * @return
	 */
	public String getXML() {
		String signStr = "amount=" + this.amount;
		String xml = "<xml>" + "<amount>" + this.amount + "</amount>";

		if (StringUtils.isNotBlank(this.checkName)) {
			xml +=  "<check_name>"+this.checkName + "</check_name>";
			signStr += "&check_name=" + this.checkName;
		}

		xml += "<desc>" + this.desc + "</desc>";
		signStr += "&desc=" + this.desc ;


		xml += "<mch_appid>" + this.mchAppId + "</mch_appid>";
		signStr += "&mch_appid=" + this.mchAppId ;

		xml += "<mchid>" + this.mchId + "</mchid>";
		signStr += "&mchid=" + this.mchId;



		xml +=  "<nonce_str>"+this.nonceStr + "</nonce_str>";
		signStr += "&nonce_str=" + this.nonceStr;

		xml +=  "<openid>"+this.openId + "</openid>";
		signStr += "&openid=" + this.openId;

		xml +=  "<partner_trade_no>"+this.partnerTradeNo + "</partner_trade_no>";
		signStr += "&partner_trade_no=" + this.partnerTradeNo;



		if (StringUtils.isNotBlank(this.reUserName)) {
			xml +=  "<re_user_name>"+this.reUserName + "</re_user_name>";
			signStr += "&re_user_name=" + this.reUserName;
		}
		xml +=  "<spbill_create_ip>"+this.spbillCreateIp + "</spbill_create_ip>";
		signStr += "&spbill_create_ip=" + this.spbillCreateIp;


		//2018年2月8日，加上企业付款接口里面没有说明的key
		signStr+="&key="+key;
		System.out.println("--------微信企业付款签名字符串------------"+signStr);
		//xml+="</xml>";
		signStr = Digests.md5(signStr).toUpperCase();

		this.sign = signStr;
		xml += "<sign>" + signStr + "</sign>" + "</xml>";
		return xml;
	}
	/*public static void main(String[] args)
	{
		char c ='_';
		System.out.println((int)c);
	}*/
}
